package com.achieve.mqtt.service.impl;

import com.achieve.mqtt.domain.TbLamp;
import com.achieve.mqtt.mapper.TbLampMapper;
import com.achieve.mqtt.service.TbLampService;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author z6001
 * @description 针对表【tb_lamp】的数据库操作Service实现
 * @createDate 2025-04-20 17:15:03
 */
@Service
public class TbLampServiceImpl extends ServiceImpl<TbLampMapper, TbLamp>
        implements TbLampService {

    @Override
    public void updateLampOnlineStatus(String payload) {
        Map<String, Object> map = JSON.parseObject(payload, Map.class);
        String deviceId = map.get("deviceId").toString();
        Integer status = Integer.parseInt(map.get("status").toString());
        TbLamp tbLamp = super.lambdaQuery().eq(TbLamp::getDeviceid, deviceId)
                .one();
        if (tbLamp == null) {
            tbLamp = new TbLamp();
            tbLamp.setDeviceid(deviceId);
            tbLamp.setStatus(status);
            tbLamp.setCreatetime(new Date());
            tbLamp.setUpdatetime(new Date());
            super.save(tbLamp);
        } else {
            tbLamp.setStatus(status);
            tbLamp.setUpdatetime(new Date());
            super.updateById(tbLamp);
        }
    }
}




